package boxe.timer.tools;

/**
 * Serialize or deserialize java object in xml format
 * 
 * @author julien Perriard
 * @date 18.05.2010
 * 
 */

import java.beans.PersistenceDelegate;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import boxe.timer.BoxeTimer;
import boxe.timer.Mode;

public class XmlTools {

	/**
	 * Constructor
	 * 
	 */
    public XmlTools() {
    }
	
   /**
	* Serialize object into  file
	*
    */
    public  void encodeToFile(Object object, String fileName) throws FileNotFoundException, IOException {
        XMLEncoder encoder = new XMLEncoder(new FileOutputStream(fileName));
        try {
            encoder.writeObject(object);
            encoder.flush();
        } finally {
            encoder.close();
        }
    }
    
    /**
     * Deserialize from file to object;
     * 
     * @param fileName
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     */
    public  Object decodeFromFile(String fileName) throws FileNotFoundException, IOException {
        Object object = null;
        // ouverture de decodeur
        //BufferedReader bf = new BufferedReader(		
        	//	new InputStreamReader(getClass().getResourceAsStream(fileName)));
        XMLDecoder decoder = new XMLDecoder(new FileInputStream(fileName));
        try {
            object = decoder.readObject();
        } finally {
            decoder.close();
        }
        return object;
    }
}
